package com.shop.cereshop.commons.tencent.im.factory;

import com.shop.cereshop.commons.tencent.im.enums.im.MsgTypeEnum;
import com.shop.cereshop.commons.tencent.im.model.msg.data.*;
import com.shop.cereshop.commons.tencent.im.model.msg.element.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.EnumMap;
import java.util.Map;

/**
 * @description: 消息元素构建工厂
 * @author zdd
 * @date 2025/9/9 21:41
 * @version 1.0
 */
@Slf4j
@Component
public class MessageElementFactory {

    private final Map<MsgTypeEnum, MsgElementBuilder> builderMap;

    public MessageElementFactory() {
        this.builderMap = createBuilderMap();
    }

    private Map<MsgTypeEnum, MsgElementBuilder> createBuilderMap() {
        Map<MsgTypeEnum, MsgElementBuilder> map = new EnumMap<>(MsgTypeEnum.class);
        map.put(MsgTypeEnum.TEXT, this::buildTextMsgElement);
        map.put(MsgTypeEnum.CUSTOM, this::buildCustomMsgElement);
        map.put(MsgTypeEnum.FACE, this::buildFaceMsgElement);
        map.put(MsgTypeEnum.LOCATION, this::buildLocationMsgElement);
        map.put(MsgTypeEnum.IMAGE, this::buildImageMsgElement);
        map.put(MsgTypeEnum.FILE, this::buildFileMsgElement);
        map.put(MsgTypeEnum.VIDEO_FILE, this::buildVideoMsgElement);
        map.put(MsgTypeEnum.SOUND, this::buildSoundMsgElement);
        return Collections.unmodifiableMap(map);
    }

    public TIMMsgElement buildMsgElement(MsgTypeEnum msgTypeEnum, MsgData data) {
        if (msgTypeEnum == null) {
            log.error("消息类型不能为空");
            return null;
        }

        MsgElementBuilder builder = builderMap.get(msgTypeEnum);
        if (builder == null) {
            log.warn("未配置构建器的消息类型: {}", msgTypeEnum);
            return null;
        }

        return builder.build(data);
    }

    private TIMMsgElement buildTextMsgElement(MsgData data) {
        // 文本消息只需要文本内容
        if (data instanceof TextMsgData) {
            return new TIMTextMsgElement(((TextMsgData) data).getText());
        }
        log.warn("文本消息数据类型不匹配: {}", data.getClass().getName());
        return null;
    }

    private TIMMsgElement buildCustomMsgElement(MsgData data) {
        if (data instanceof CustomMsgData) {
            CustomMsgData customData = (CustomMsgData) data;
            TIMCustomMsgElement.CustomMsgContent msgContent = new TIMCustomMsgElement.CustomMsgContent(
                    customData.getData(), customData.getDesc(), customData.getExt(), customData.getSound());
            return new TIMCustomMsgElement(msgContent);
        }
        log.warn("自定义消息数据类型不匹配: {}", data.getClass().getName());
        return null;
    }

    private TIMMsgElement buildFaceMsgElement(MsgData data) {
        if (data instanceof FaceMsgData) {
            FaceMsgData faceData = (FaceMsgData) data;
            return new TIMFaceMsgElement(faceData.getIndex(), faceData.getData());
        }
        log.warn("表情消息数据类型不匹配: {}", data.getClass().getName());
        return null;
    }

    private TIMMsgElement buildLocationMsgElement(MsgData data) {
        if (data instanceof LocationMsgData) {
            LocationMsgData locationData = (LocationMsgData) data;
            return new TIMLocationMsgElement(locationData.getDesc(), locationData.getLatitude(), locationData.getLongitude());
        }
        log.warn("位置消息数据类型不匹配: {}", data.getClass().getName());
        return null;
    }

    private TIMMsgElement buildImageMsgElement(MsgData data) {
        if (data instanceof ImageMsgData) {
            ImageMsgData imageData = (ImageMsgData) data;
            return new TIMImageMsgElement(imageData.getUuid(), imageData.getImageFormat(), imageData.getImageInfoArray());
        }
        log.warn("图像消息数据类型不匹配: {}", data.getClass().getName());
        return null;
    }

    private TIMMsgElement buildFileMsgElement(MsgData data) {
        if (data instanceof FileMsgData) {
            FileMsgData fileData = (FileMsgData) data;
            return new TIMFileMsgElement(fileData.getUrl(), fileData.getUuid(), fileData.getFileSize(),
                    fileData.getFileName(), fileData.getDownloadFlag());
        }
        log.warn("文件消息数据类型不匹配: {}", data.getClass().getName());
        return null;
    }

    private TIMMsgElement buildVideoMsgElement(MsgData data) {
        if (data instanceof VideoMsgData) {
            VideoMsgData videoData = (VideoMsgData) data;
            return new TIMVideoFileMsgElement(
                    videoData.getVideoUrl(), videoData.getVideoUuid(), videoData.getVideoSize(), videoData.getVideoSecond(),
                    videoData.getVideoFormat(), videoData.getVideoDownloadFlag(), videoData.getThumbUrl(), videoData.getThumbUuid(),
                    videoData.getThumbSize(), videoData.getThumbWidth(), videoData.getThumbHeight(), videoData.getThumbFormat(),
                    videoData.getThumbDownloadFlag());
        }
        log.warn("视频消息数据类型不匹配: {}", data.getClass().getName());
        return null;
    }

    private TIMMsgElement buildSoundMsgElement(MsgData data) {
        if (data instanceof SoundMsgData) {
            SoundMsgData soundData = (SoundMsgData) data;
            return new TIMSoundMsgElement(soundData.getUrl(), soundData.getUuid(), soundData.getSize(),
                    soundData.getSecond(), soundData.getDownloadFlag());
        }
        log.warn("语音消息数据类型不匹配: {}", data.getClass().getName());
        return null;
    }
}
